package com.koala.mylittlesecretary;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	public static Activity mainAct;

	// android specific control classes
	AudioManager myAM;

	// variables & instances
	Settings mySettings;

	ContactItems myCI;
	ContactReader myCR;

	LoadAndSave myLoadAndSave;

	// layout
	Button btImportContacts;
	Button btStartService;
	Button btStopService;
	TextView txtInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.v("MLS", "MLS started!");

		// for all the other classes
		mainAct = this;

		// android specific
		myAM = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);

		// Settings
		mySettings = new Settings();
		myCR = new ContactReader();
		myLoadAndSave = new LoadAndSave();

		if (mySettings.bFirstStart) {
			// load from phone
			myCI = myCR.readContacts();
			Log.v("MLS", "Anzahl: " + myCI.hmNumberToName.size());
		} else {
			// load from saved data
			myCI = myLoadAndSave.deserializeCI();
			Log.v("MLS", "Not the first start, loading from saved data");
			Log.v("MLS", "Size: " + myCI.hmNumberToName.size());
		}

		// Buttons
		btImportContacts = (Button) findViewById(R.id.btImportContacts);

		btImportContacts.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.v("MLS", "Reimporting contacts");
				myCI = myCR.readContacts();
			}
		});

		btStartService = (Button) findViewById(R.id.btStartService);

		btStartService.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mySettings.bActive = true;
				txtInfo.setText("ACTIVE for " + myCI.hmNumberToProfile.size()
						+ " numbers - MLS controls the ringtone now");
				myAM.setRingerMode(AudioManager.RINGER_MODE_SILENT);
				mySettings.savePrefs();

			}
		});

		btStopService = (Button) findViewById(R.id.btStopService);

		btStopService.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mySettings.bActive = false;
				txtInfo.setText("INACTIVE");
				myAM.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
				mySettings.savePrefs();
			}
		});

		// TextView
		txtInfo = (TextView) findViewById(R.id.txtInfo);

		if (mySettings.bActive)
			txtInfo.setText("ACTIVE for " + myCI.hmNumberToProfile.size()
					+ " numbers - MLS controls the ringtone now");
		else
			txtInfo.setText("INACTIVE");

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// save variables
		myLoadAndSave.serializeCI(myCI);
		// save preferences
		mySettings.savePrefs();
	}

}
